<template>
  <div class="app-container">
    <el-card class="content-edit">
      <div slot="header">{{ pageType==='add'?'添加网点':'编辑网点' }}</div>
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="网点名称" maxlength="20">
          <el-input v-model="form['name']"/>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="form['mobile']"/>
        </el-form-item>
        <el-form-item label="工作时间">
          <el-time-picker
            v-model="form['businessHours']"
            is-range
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            placeholder="选择工作时间"
            value-format="HH:mm"/>
        </el-form-item>
        <el-form-item label="网点地区">
          <el-select v-model="form['provinceCodeIdx']" placeholder="请选择省份"
                     @change="provinceSelectChange">
            <el-option v-for="(item,index) in provinceList" :key="item.code" :label="item.name" :value="index"/>
          </el-select>
          <el-select v-if="cityList.length!=0" v-model="form['cityCodeIdx']" placeholder="请选择市"
                     @change="citySelectChange">
            <el-option v-for="(item,index) in cityList" :key="item.code" :label="item.name" :value="index"/>
          </el-select>
          <el-select v-if="areaList.length!=0" v-model="form['areaCode']" placeholder="请选择区">
            <el-option v-for="item in areaList" :key="item.code" :label="item.name" :value="item.code"/>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input v-model="form['address']" :rows="3" type="textarea"/>
        </el-form-item>
        <el-form-item label="网点评分">
          <el-rate v-model="form['score']" style="margin-top: 12px;"/>
        </el-form-item>
        <el-form-item label="网点实景图">
          <ws-upload-multiple-img v-model="form['picId']" :path="form['picUrl']" :limit="6"/>
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" type="primary" @click="onSubmit">保存</el-button>
          <el-button @click="$router.back()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  export default {
    name: 'RentCarBranchModify',
    data() {
      return {
        pageType: 'add',
        loading: false,
        // 省市区列表
        provinceList: [],
        // 市列表
        cityList: [],
        // 地区列表
        areaList: [],

        form: {
          // 网点名称
          name: '',
          // 联系电话
          mobile: '',
          // 省
          provinceCodeIdx: '',
          provinceCode: '',
          // 市
          cityCodeIdx: '',
          cityCode: '',
          // 区
          areaCode: '',
          // 详细地址
          address: '',
          // 工作时间
          businessHours: '',
          // 评分
          score: 5,
          // 图片
          picId: '',
          picUrl: [],
          // 上门服务费
          serviceFee: '',
          // 上门范围
          deliverRange: ''
        }
      }
    },
    mounted(){
      this.form['id'] = this.$router.currentRoute.query['id'] || '';
      if (this.$router.currentRoute.path.indexOf('add') > 0) {
        this.pageType = 'add'
      } else {
        this.pageType = 'edit'
      }
      this.loadProvinceList();
    },
    methods: {
      // 加载省市区列表
      loadProvinceList(){
        (async () => {
          const result = await this.$service.applicationGetProvinceList();
          if (result.code === 0) {
            this.provinceList = result.data;
            if (this.pageType === 'edit') {
              this.loadData();
            }
          }
        })();
      },
      // 加载路线信息
      loadData(){
        (async () => {
          const result = await this.$service.applicationGetRentCarBranchDetails({id: this.form['id']});
          if (result.code === 0) {
            const data = result.data || {};
            for (let i = 0; i < this.provinceList.length; i++) {
              if (this.provinceList[i].code === data['provinceCode']) {
                data['provinceCodeIdx'] = i;
                this.cityList = this.provinceList[i]['children'];
                for (let j = 0; j < this.provinceList[i]['children'].length; j++) {
                  if (this.provinceList[i]['children'][j].code === data['cityCode']) {
                    data['cityCodeIdx'] = j;
                    this.areaList = this.provinceList[i]['children'][j]['children'] || [];
                  }
                }
              }
            }
            data['businessHours'] && (data['businessHours'] = data['businessHours'].split('-'));
            data['picId'] = data['picId'].join(',');
            Object.assign(this.form, data);
          }
        })();
      },
      // 省份选择监听e:选择的index,params:出发地还是终点
      provinceSelectChange(e){
        // console.log(e);
        this['cityList'] = this.provinceList[e]['children'];
        this.form['provinceCode'] = this.provinceList[e]['code'];
        this.form['cityCodeIdx'] = '';
        this.form['cityCode'] = '';
        this.form['areaCode'] = '';
      },
      // 市级选择监听e:选择的index,params:出发地还是终点
      citySelectChange(e){
        // console.log(e);
        this['areaList'] = this['cityList'][e]['children'] || [];
        this.form['cityCode'] = this['cityList'][e]['code'];
        this.form['areaCode'] = '';
      },
      // 提交
      onSubmit() {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.loading = true;
            (async () => {
              const params = Object.assign({}, this.form);
              // console.log(params);
              params.businessHours = params.businessHours.join('-');
              let result = null;
              if (this.pageType === 'add') {
                result = await this.$service.applicationAddRentCarBranch(params);
              } else {
                result = await this.$service.applicationModifyRentCarBranch(params);
              }
              this.loading = false;
              if (result.code === 0) {
                this.$message.success('操作成功~');
                this.$router.back();
              } else {
                this.$message.error(result.msg);
              }
            })();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>

<style lang="scss" type="text/scss" scoped>
  @import '../../../styles/index.scss';

</style>
